<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 17:14:35 apple $
*/
namespace core;
defined('BY_JYA') or exit('error');
class wxapp{
	
	public function WXBizDataCrypt($appid, $sessionKey)
	{
		$this->sessionKey = $sessionKey;
		$this->appid = $appid;
	}
	public function pkcs7Encode($encrypt_data, $iv) {
		$key = base64_decode($this->sessionKey);
		$result = aes_pkcs7_decode($encrypt_data, $key, $iv);
		if (is_error($result)) {
			exi('解密失败!','error');
		}
		$result = json_decode($result, true);
		if (empty($result)) {
			exi('解密失败!','error');
		}
		if ($result['watermark']['appid'] != $this->appid) {
			exi('解密失败','error');
		}
		unset($result['watermark']);
		return $result;
	}
	public function appcode($path,$appid,$secret,$width=430){
		//$path='/'.$plugin.'/'.$action.'/index';
		$u='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
		$re=cfc('http')->ihttp_get($u);
		$re=json_decode($re['content'],true);
		$access_token=$re['access_token'];
		if(!$access_token){
			return '';
		}
		$url='https://api.weixin.qq.com/wxa/getwxacode?access_token='.$access_token;
		//$url='https://api.weixin.qq.com/wxa/getwxacode?access_token={access_token}';
		$post=array('path'=>$path,'width'=>$width);
		$post=ijson_encode($post);
		$result=cfc('http')->ihttp_post($url,$post);
		return $result['content'];
	}
	
}
require ROOT_D.'/core/library/pkcs7/pkcs7Encoder.php';